import { Component, Injector, ViewChild, Input } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';

import { WorkflowServiceProxy, UserServiceProxy, GetWorkflowInstanceViewDto, SupplierQuotationServiceProxy, SupplierQuotationModel } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import * as _ from 'lodash';
import * as moment from 'moment';
import { from } from 'rxjs';
import { type } from 'os';

@Component({
    selector: 'awaitQuatationSupplier',
    templateUrl: './AwaitQuatationSupplier.component.html',
    styleUrls: ['./AwaitQuatationSupplier.component.css']
})
export class AwaitQuatationSupplierComponent extends AppComponentBase {
    @Input() suppliername: any;
    @Input() workflowid: any;
    @Input() editEnable: any;

    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    isLoading = true;
    rows: number;
    supplierquotations: SupplierQuotationModel[];
    clonedOutsourcings: { [s: number]: SupplierQuotationModel; } = {};
    isEdit: boolean;
    constructor(injector: Injector,
        private _supplierquotationService: SupplierQuotationServiceProxy) { super(injector); }

    ngOnInit() {

        console.log(this.editEnable);
        this.isEdit = this.editEnable;
        this.initForm();
    }
    get multiTenancySideIsTeanant(): boolean {
        return this.editEnable === 'true';
    }


    initForm() {
        console.log(this.workflowid);
        this.isLoading = false;
    }

    getOutsourings(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);

            return;
        }
        this.primengTableHelper.showLoadingIndicator();

        this._supplierquotationService.getSupplierQuotations(this.workflowid,
            this.suppliername,
            this.workflowid,
            '',
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event))
            .pipe(
                finalize(() => this.primengTableHelper.hideLoadingIndicator())
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });

    }



    onRowEditInit(quotationmodel: SupplierQuotationModel) {
        console.log(quotationmodel);
        this.clonedOutsourcings[quotationmodel.taskForModelId.toString()] = { ...quotationmodel };
    }

    onRowEditSave(quotationmodel: SupplierQuotationModel) {


        console.log(quotationmodel);
        if (quotationmodel.outsourcingAmount > 0) {
            delete this.clonedOutsourcings[quotationmodel.taskForModelId.toString()];

            this._supplierquotationService.createOrUpdateSupplierQuotation(quotationmodel).subscribe(result => {
                if (result === 'ok') {
                    this.notify.info(this.l('SavedSuccessfully'));
                }


            })
        } else {
            this.notify.info('申请数量不能小于0');
        }
    }

    onRowEditCancel(quotationmodel: SupplierQuotationModel, index: number) {
        console.log(this.clonedOutsourcings[quotationmodel.taskForModelId.toString()]);
        this.supplierquotations[index] = this.clonedOutsourcings[quotationmodel.taskForModelId.toString()];
        delete this.supplierquotations[quotationmodel.taskForModelId];
    }

}
